//
//  QNLimitTextView.m
//  QNView
//
//  Created by QNRZMac on 15/5/7.
//  Copyright (c) 2015年 Sunline. All rights reserved.
//

#import "JFLimitTextView.h"
#import "JFTextView.h"
#import <JFBase/JFBase.h>
@interface JFLimitTextView ()<UITextViewDelegate>


@end

@implementation JFLimitTextView



#pragma mark - life Circle

-(instancetype)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}


-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];

    _textView.frame = CGRectMake(0, 0, rect.size.width, rect.size.height-20);
    
    NSString * limitStr = [@(_curLimitNum/2) stringValue];
    CGSize size = [limitStr sizeWithAttributes:@{NSFontAttributeName:self.tipsFont}];
    
    self.cornerLabel.text = [@(_curLimitNum/2) stringValue];
    self.cornerLabel.frame = CGRectMake(rect.size.width -size.width-5, rect.size.height-size.height-2 , size.width,size.height);
}


#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {
    if (textView.markedTextRange == nil && self.limitNum > 0 && [self getToInt:textView.text] > self.limitNum) {
        [textView deleteBackward];
    }
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([self.delegate respondsToSelector:@selector(limitTextViewShouldBeginEditing:)]) {
        return [self.delegate limitTextViewShouldBeginEditing:self];
    }
    return YES;
}



-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if ([self.delegate respondsToSelector:@selector(limitTextViewShouldEndEditing:)]) {
        return [self.delegate limitTextViewShouldEndEditing:self];
    }
    return YES;
}

#pragma mark - OverRide

- (BOOL)resignFirstResponder{
    return [self.textView resignFirstResponder];
}

#pragma mark - Notification Respone

- (void)textDidChange{
    _curLimitNum = _limitNum  - [self getToInt:self.textView.text];
    _cornerLabel.text = [@(_curLimitNum/2) stringValue];
    if (_curLimitNum<0) {
        _cornerLabel.textColor = [UIColor redColor];
        _isOutOfRange = YES;
        self.cornerLabel.text = [@(0/2) stringValue];
    }else{
        _cornerLabel.textColor = [UIColor lightGrayColor];
        _isOutOfRange = NO;
        self.cornerLabel.text = [@(_curLimitNum/2) stringValue];
    }
    [self outOfRangeRemind:_isOutOfRange];
    NSString * limitStr = [@(_curLimitNum/2) stringValue];
    CGSize size = [limitStr sizeWithAttributes:@{NSFontAttributeName:self.tipsFont}];
    
    
    self.cornerLabel.frame = CGRectMake(self.frame.size.width -size.width-5, self.frame.size.height-size.height-2 , size.width,size.height);
    
    if ([self.delegate respondsToSelector:@selector(limitTextViewTextDidChange:)]) {
        [self.delegate limitTextViewTextDidChange:self];
    }
}


- (void)outOfRangeRemind:(BOOL)isOutOfRange{
    if ([self.delegate respondsToSelector:@selector(limitTextViewTextIsOutOfRange:)]) {
        [self.delegate limitTextViewTextIsOutOfRange:isOutOfRange];
    }
}

//得到中英文混合字符串长度 方法2
- (int)getToInt:(NSString*)strtemp

{
    int legthCount = 0;
    for(int i = 0 ;i<strtemp.length;i++){
        unichar c = [strtemp characterAtIndex:i];
        if ((c >=0x4E00 && c <=0x9FFF) || (c >= 0x2E80 && c <= 0xFE4F) || (c >=0xff01 && c <= 0xff5e))
        {
            //printf("汉字");
            legthCount +=2 ;
        }
        else
        {
            //printf("英文");
            legthCount += 1;
        }
    }
    return legthCount;
}

#pragma Setter & Getter

-(void)setLimitNum:(NSInteger)limitNum{
    if (limitNum == 99999) {
        _cornerLabel.hidden = YES;
    }
    _limitNum = limitNum;
    _curLimitNum = limitNum;
    [self textDidChange];
    [self.textView limitTextLength:limitNum numberOfChineseChar:2];
}


-(UILabel *)cornerLabel{
    if (!_cornerLabel) {
        _cornerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
        _cornerLabel.textColor = [UIColor lightGrayColor];
        _cornerLabel.font = self.tipsFont;
        [self addSubview:_cornerLabel];
    }
    return _cornerLabel;
}


-(UITextView *)textView{
    if (!_textView) {
        _textView = [[JFTextView alloc] initWithFrame:CGRectZero];
        _textView.delegate = self;
        [self addSubview:_textView];
    }
    return _textView;
}


-(void)setText:(NSString *)text{
    self.textView.text = text;
    [self textDidChange];
}

-(NSString *)text{
    return self.textView.text;
}

-(UIFont *)tipsFont{
    if (!_tipsFont) {
        _tipsFont = [UIFont systemFontOfSize:14];
    }
    return _tipsFont;
}

-(void)setTextFont:(UIFont *)textFont{
    _textView.font = textFont;
}


@end
